package com.yun.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.yun.pojo.ImageData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController 
public class TestController1 {
    @RequestMapping(value = "export", method = RequestMethod.POST)
    @ResponseBody
    public void  chartExport(HttpServletResponse response, String base64Info, HttpServletRequest request) throws Exception {
        String newFileName;
        newFileName = "统计图" + System.currentTimeMillis() + ".pdf";
        String newPngName = newFileName.replaceFirst(".pdf", ".png");
        String exportFilePath = "d:/test";
        String filepath = exportFilePath + "/" + newPngName;
        base64Info = base64Info.replaceAll(" ", "+");
        BASE64Decoder decoder = new BASE64Decoder();
        String[] arr = base64Info.split("base64,");
        byte[] buffer;
        try {
            buffer = decoder.decodeBuffer(arr[1]);
        } catch (IOException e) {
            throw new RuntimeException();
        }
        OutputStream output = null;
        try {
            output = new FileOutputStream(new File(filepath));//生成png文件
            output.write(buffer);
            output.flush();
            output.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String fileName="test";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        // 如果使用流 记得关闭
        InputStream inputStream = null;
        try {
            List list = new ArrayList();
            ImageData imageData = new ImageData();
            imageData.setFile(new File(filepath));
            list.add(imageData);
            WriteSheet writeSheet = EasyExcel.writerSheet(0, "all")
                    .head(ImageData.class).build();
            excelWriter.write(list, writeSheet);
            excelWriter.finish();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if(output!=null){
                output.close();
            }
            if(excelWriter!=null){
                excelWriter.finish();
            }
        }

    }


}

